#!/usr/bin/env python
""" guimain

    The main entry point for the developer GUI.
    Hopefully a real GUI one day.
"""

import sys, logging, os, os.path

def setupEnviron():
    """ Locate the game root and adjust the python search path. """
    # Setup the python path so it finds all the modules.
    if not "PGAME_HOME" in os.environ:
        curpath = os.getcwd()
        while curpath:
            if os.path.exists(os.path.join(curpath, "src")):
                break
            curpath, tail = os.path.split(curpath)
        sys.path.append(os.path.join(curpath, "src"))
        os.environ["PGAME_HOME"] = curpath
    else:
        curpath = os.environ["PGAME_HOME"]
    
    # Make sure we can find all of our resources
    os.environ["PGAME_IMAGES"] = os.path.join(curpath, "images")
    os.environ["PGAME_FONTS"] = os.path.join(curpath, "fonts")
    os.environ["PGAME_MAPS"] = os.path.join(curpath, "maps")
    os.environ["PGAME_LOGS"] = os.path.join(curpath, "logs")

def run():
    """ Run the developer GUI. """
    from gui.mainWindow import MainWindow
    from pgEngine.game import PirateGame
    from gui.resources import Resources
    from pgEngine.point import Point
    
    logFormat = '%(asctime)s %(name)-30s %(levelname)-8s %(message)s'
    logging.basicConfig(level=logging.DEBUG,
                        format=logFormat,
                        datefmt='%d-%m-%y %H:%M',
                        filename=os.path.join(os.environ["PGAME_LOGS"],
                                              'pgameDebugConsole.log'),
                        filemode='w')
    
    res = Resources()
    game = PirateGame()
    
    game.ocean.load(res=res, mapName="map1.txt")
    
    game.addPlayer()
    game.ocean.moveItemTo(game.players[0].boats[0], Point(145.0, -80.0))
    game.addPlayer()
    game.ocean.moveItemTo(game.players[1].boats[0], Point(150.0, -75.0))
    game.start()

    mainWindow = MainWindow(gameObj=game, resources=res)
    mainWindow.run()
    
    game.ocean.save(res=res, mapName="map1.txt")

if __name__ == "__main__":
    setupEnviron()
    run()